﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Util.IO;

namespace PlanningGIS.Framework
{
    /// <summary>
    /// 资源操作类
    /// </summary>
    public class ResourcesHelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        protected ResourcesHelper()
        {

        }

        /// <summary>
        /// 根据文件名得到图标对象
        /// </summary>
        /// <param name="name">带相对路径的文件名称</param>
        /// <returns></returns>
        public static Icon GetIcon(string name)
        {
            name = GetFullName(name);
            if (string.IsNullOrWhiteSpace(name)) return null;
            return new Icon(name);
        }

        /// <summary>
        /// 根据文件名获取图片
        /// </summary>
        /// <param name="name">带相对路径的文件名称</param>
        /// <returns></returns>
        public static Image GetImage(string name)
        {
            name = GetFullName(name);
            if (string.IsNullOrWhiteSpace(name) || !File.Exists(name)) return null;
            return new Bitmap(name);
        }

        /// <summary>
        /// 根据文件名得到指针对象
        /// </summary>
        /// <param name="name">带相对路径的文件名称</param>
        /// <returns></returns>
        public static Cursor GetCursor(string name)
        {
            name = GetFullName(name);
            if (string.IsNullOrWhiteSpace(name)) return null;
            return new Cursor(name);
        }

        ///// <summary>
        ///// 得到文件的字节视图
        ///// </summary>
        //private static Stream LoadStream(string filename)
        //{
        //    Assembly assembly = Assembly.GetAssembly(typeof(ResourcesHelper));
        //    string name = String.Format("PlanningGIS.Resources.{0}", filename);
        //    return assembly.GetManifestResourceStream(name);
        //}

        /// <summary>
        /// 获取图片文件的全路径
        /// </summary>
        /// <param name="name">带相对路径的文件名称</param>
        /// <returns></returns>
        private static string GetFullName(string name)
        {
            if (string.IsNullOrWhiteSpace(name)) return "";
            name = name.Trim().Replace("/", "\\");
            if (name.StartsWith("\\")) name = name.Substring(1);
            return PathHelper.GetDllPath() + "\\Images\\" + name;
        }
    }


}
